<template>
  <div id="app">

    <router-view/>
    <bottom-person-nav/>

  </div>
</template>

<script>
  import bottomNav from '@/components/person-nav.vue'
  import Vue from 'vue'
  import {NavBar} from 'vant';
  import common from './common/common';
  import { login } from './api/login/index';
  import { Toast} from 'vant';

  export default {
    name: 'App',
    components: {
      'bottom-person-nav': bottomNav
    },
    watch:{
      $route(){
        this.init()
      }
    },

    async created(){

      this.init()
      if (true) {
          return;
      }
      let userInfo = common.store.getUser();
      if (!userInfo) {
        const code = this.GetQueryString('code');
        if (code) {
          const res1 = await login({code, state: 'success'}).catch(() => {
            Toast('网络出了点小差，请稍后重试');
          });
          if (res1 && res1.data && res1.data.code === 200) {
            // 保存用户信息
            const resData = res1.data.data;
            common.store.setUser(resData);
            this.$router.push('/');
          } else {
            Toast('网络出了点小差，请稍后重试');
          }
        } else {
          const appid = 'wx838cf54dd030d93a';
          const redirect_uri = encodeURIComponent('http://www.zxskill.com');
          const scope = 'snsapi_userinfo';
          const href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${redirect_uri}&response_type=code&scope=${scope}&state=success#wechat_redirect`;
          window.location.href = href;
        }
      }
    },
    methods: {
      GetQueryString(name) {
        const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
        const r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
      },
      init(){
        let timestamp = new Date().getTime();
        let nonce = '1341afaf'
        this.axios.get('/api/wx/share1').then(res=>{
          this.axios.get('/api/wx/share2',{params:{token:res.data.access_token}}).then(res=>{
            console.log(res.ticket)
            this.axios.get('/api/wx/share3',{params:{
                jsapi_ticket:res.data.ticket,url:window.location.href
              }}).then(rr=>{
              console.log(rr)
              console.log(rr.data)
              wx.config({
                // debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                appId: rr.data.oppId, // 必填，公众号的唯一标识
                timestamp: rr.data.timestamp, // 必填，生成签名的时间戳
                nonceStr: rr.data.noncestr, // 必填，生成签名的随机串
                signature: rr.data.signature,// 必填，签名
                jsApiList: [
                  'onMenuShareTimeline',//分享朋友圈
                  'onMenuShareAppMessage',//分享给好友
                  'onMenuShareQQ',//分享到QQ
                  'onMenuShareWeibo',//分享腾讯微博
                ] // 必填，需要使用的JS接口列表
              });
              let tmp = {
                title: '欢迎来到知行', // 分享标题
                desc: '来知行,做兼职,快乐一下', // 分享描述
                link: window.location.href, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                imgUrl: 'http://m.zxskill.com/static/img/qsmy4.jpg', // 分享图标
              }
              wx.ready(function () {   //需在用户可能点击分享按钮前就先调用
                wx.onMenuShareTimeline(tmp, function(res) {
                });
                wx.onMenuShareAppMessage(tmp, function(res) {
                });
                wx.onMenuShareQQ(tmp, function(res) {
                });
                wx.onMenuShareWeibo(tmp, function(res) {
                });
              });
            })
          })
        }).catch(e=>{
          console.log(e)
        })
      }
    },

  };
</script>



